﻿#region Copyright (c) Nicola Gallo All rights reserved.
/*
 * All rights reserved. Reproduction of trasmission of this file, or a portion thereof,
 * is forbidden without prior written permission of Nicola Gallo.
 * 
 * Author: Nicola Gallo
 * Date: 09/04/2011
 * 
 * */
#endregion
namespace PrismMVC.Shell.Containers
{
    //.NET References
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Controls;

    //Microsoft References
    using Microsoft.Practices.Prism.Modularity;
    using Microsoft.Practices.Prism.Logging;
    using Microsoft.Practices.Prism.Events;
    using Microsoft.Practices.Unity;

    //PrismMVC References
    using PrismMVC.Shell.Data;

    /// <summary>
    /// Defines a view.
    /// </summary>
    public class View : UserControl, IView, ISupportViewData
    {
        //Fields

        private ViewDataDictionary _viewData;
        private IList<IPresentationModel> _presentationModels;

        //Constructors

        /// <summary>
        /// Default constructor.
        /// </summary>
        public View() { }

        /// <summary>
        /// Creates a new instance of <see cref="View"/>.
        /// </summary>
        /// <param name="unityContainer">Unity container.</param>
        /// <param name="loggerFacade">Logger facade.</param>
        /// <param name="eventAggregator">Event aggregator.</param>
        public View(IUnityContainer unityContainer, ILoggerFacade loggerFacade, IEventAggregator eventAggregator)
        {
            if (unityContainer == null)
            {
                throw new ArgumentNullException("UnityContainer parameter can not be equals to null.");
            }
            else if (loggerFacade == null)
            {
                throw new ArgumentNullException("LoggerFacade parameter can not be equals to null.");
            }
            else if (eventAggregator == null)
            {
                throw new ArgumentNullException("EventAggregator parameter can not be equals to null.");
            }
            this.UnityContainer = unityContainer;
            this.LoggerFacade = loggerFacade;
            this.EventAggregator = eventAggregator;
        }

        //Properties

        /// <summary>
        /// Gets <see cref="ViewData"/> identifier.
        /// </summary>
        public string ViewDataID { get; private set; }

        /// <summary>
        /// Gets the <see cref="ViewData"/>.
        /// </summary>
        public ViewDataDictionary ViewData
        {
            get
            {
                if (this._viewData == null)
                {
                    this._viewData = ViewDataDictionaryFactory.CreateViewDataDictionary(this.ViewDataID);
                }
                return this._viewData;
            }
        }

        /// <summary>
        /// Gets the shell proxy.
        /// </summary>
        protected IUnityContainer UnityContainer { get; set; }

        /// <summary>
        /// Gets the logger facade.
        /// </summary>
        protected ILoggerFacade LoggerFacade { get; private set; }

        /// <summary>
        /// Gets the event aggregator.
        /// </summary>
        protected IEventAggregator EventAggregator { get; private set; }

        /// <summary>
        /// Gets presentation models.
        /// </summary>
        protected internal IList<IPresentationModel> PresentationModels 
        { 
            get 
            {
                if (this._presentationModels == null)
                {
                    this._presentationModels = new List<IPresentationModel>();
                }
                return this._presentationModels;
            } 
        }

        //Methods

        /// <summary>
        /// Sets the <see cref="ViewData"/> identifier.
        /// </summary>
        /// <param name="ViewDataID">ViewDataDictionary identifier.</param>
        internal void SetViewDataID(String ViewDataID)
        {
            if (string.IsNullOrWhiteSpace(ViewDataID))
            {
                throw new ArgumentException("ViewDataID paramater can not be equals to either null or empty string.");
            }
            this.ViewDataID = ViewDataID;
        }
    }
}